﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novel.Entity;
using System.Data.SqlClient;
using System.Data;
using Novel.DAL.Base;

namespace Novel.DAL
{
    public class T_002BookGatherUrl_DAL : IT_002BookGatherUrlManager
    {
       #region 表名
       public  string GetTableName()
       {
           return "T_002BookGatherUrl";
       }
       #endregion

        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(T_002BookGatherUrlEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("INSERT INTO {0}", GetTableName());
            strSql.Append("(BookID,BookName,TaskID,BookListUrl,LastChapterUrl)");
            strSql.Append(" values ");
            strSql.Append("(@BookID,@BookName,@TaskID,@BookListUrl,@LastChapterUrl)");
            strSql.Append(" SELECT @ID = @@IDENTITY");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
            cmd.Parameters.Add("@ID", SqlDbType.Int);
            cmd.Parameters["@ID"].Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = model.BookID;
            cmd.Parameters.Add("@BookName", SqlDbType.VarChar, 50);
            cmd.Parameters["@BookName"].Value = model.BookName;
            cmd.Parameters.Add("@TaskID", SqlDbType.Int, 4);
            cmd.Parameters["@TaskID"].Value = model.TaskID;
            cmd.Parameters.Add("@BookListUrl", SqlDbType.VarChar, 80);
            cmd.Parameters["@BookListUrl"].Value = model.BookListUrl;
            cmd.Parameters.Add("@LastChapterUrl", SqlDbType.VarChar, 80);
            cmd.Parameters["@LastChapterUrl"].Value = model.LastChapterUrl;
            SQLHelper da = new SQLHelper();
            int EffectRows = da.ExecuteNonQuery(cmd);
            model.ID = (int)cmd.Parameters["@ID"].Value;
            return EffectRows;
        }
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(T_002BookGatherUrlEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
            strSql.Append("BookID=@BookID,BookName=@BookName,TaskID=@TaskID,BookListUrl=@BookListUrl,LastChapterUrl=@LastChapterUrl");
            strSql.Append(" WHERE ID=@ID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
            cmd.Parameters.Add("@ID", SqlDbType.Int, 4);
            cmd.Parameters["@ID"].Value = model.ID;
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = model.BookID;
            cmd.Parameters.Add("@BookName", SqlDbType.VarChar, 50);
            cmd.Parameters["@BookName"].Value = model.BookName;
            cmd.Parameters.Add("@TaskID", SqlDbType.Int, 4);
            cmd.Parameters["@TaskID"].Value = model.TaskID;
            cmd.Parameters.Add("@BookListUrl", SqlDbType.VarChar, 80);
            cmd.Parameters["@BookListUrl"].Value = model.BookListUrl;
            cmd.Parameters.Add("@LastChapterUrl", SqlDbType.VarChar, 80);
            cmd.Parameters["@LastChapterUrl"].Value = model.LastChapterUrl;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int id)
        {
            string sql = string.Format("DELETE {0} WHERE ID=@ID", GetTableName());
            SqlCommand command = new SqlCommand(sql);
            command.Parameters.Add("@ID", SqlDbType.Int, 4);
            command.Parameters["@ID"].Value = id;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(command);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public  EntityBase FillDetailWithReader(IDataReader reader)
        {
            T_002BookGatherUrlEntity model = new T_002BookGatherUrlEntity();
            if (reader["ID"] != DBNull.Value)
                model.ID = (int)reader["ID"];
            if (reader["BookID"] != DBNull.Value)
                model.BookID = (int)reader["BookID"];
            if (reader["BookName"] != DBNull.Value)
                model.BookName = (string)reader["BookName"];
            if (reader["TaskID"] != DBNull.Value)
                model.TaskID = (int)reader["TaskID"];
            if (reader["BookListUrl"] != DBNull.Value)
                model.BookListUrl = (string)reader["BookListUrl"];
            if (reader["LastChapterUrl"] != DBNull.Value)
                model.LastChapterUrl = (string)reader["LastChapterUrl"];
            return model;
        }
        #endregion

        #region 根据ID获取一个对象
        public T_002BookGatherUrlEntity GetObjectByID(int ID)
        {
            string sql = string.Format("SELECT TOP 1 * FROM {0}  WHERE ID=@ID", GetTableName());
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Parameters.Add("@ID", SqlDbType.Int, 4);
            cmd.Parameters["@ID"].Value = ID;
            SQLHelper da = new SQLHelper();
            try
            {
                SqlDataReader reader = da.ExecuteReader(cmd);
                if (reader.Read())
                {
                    return (T_002BookGatherUrlEntity)this.FillDetailWithReader(reader);
                }
            }
            finally
            {
                da.DisConnect();
            }
            return null;
        }
        public T_002BookGatherUrlEntity GetObjectByBookID(int BookID, int TaskID)
        {
            string sql = string.Format("SELECT TOP 1 * FROM {0}  WHERE BookID=@BookID and TaskID=@TaskID", GetTableName());
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = BookID;
            cmd.Parameters.Add("@TaskID", SqlDbType.Int, 4);
            cmd.Parameters["@TaskID"].Value = BookID;
            SQLHelper da = new SQLHelper();
            try
            {
                SqlDataReader reader = da.ExecuteReader(cmd);
                if (reader.Read())
                {
                    return (T_002BookGatherUrlEntity)this.FillDetailWithReader(reader);
                }
            }
            finally
            {
                da.DisConnect();
            }
            return null;
        }
        public T_002BookGatherUrlEntity GetObjectByTaskIDandBookID(int bookID,int taskID)
        {
            string sql = string.Format("SELECT TOP 1 * FROM {0}  WHERE BookID=@BookID and TaskID=@TaskID", GetTableName());
            SqlCommand cmd = new SqlCommand(sql);
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = bookID;
            cmd.Parameters.Add("@TaskID", SqlDbType.Int, 4);
            cmd.Parameters["@TaskID"].Value = taskID;
            SQLHelper da = new SQLHelper();
            try
            {
                SqlDataReader reader = da.ExecuteReader(cmd);
                if (reader.Read())
                {
                    return (T_002BookGatherUrlEntity)this.FillDetailWithReader(reader);
                }
            }
            finally
            {
                da.DisConnect();
            }
            return null;
        }

        #endregion

        #region 数据集
        public DataTable ShowTable()
        {

            string strSql = string.Format("SELECT  * FROM {0}", GetTableName());
            return SQLHelper.Execute(strSql.ToString());
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            QueryBase manager = new QueryBase();

            entity.tbName = GetTableName();
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
    }
}
